<?php

namespace app\common\business;

use app\common\lib\Arr;
use app\common\lib\Curl;
use app\common\lib\Status;
use app\common\lib\Str;
use app\common\lib\Time;
use think\Exception;
use think\facade\Cache;

class WorkCpRecord
{

    public $model = null;

    public function __construct()
    {
        $this->model = new \app\common\model\mysql\WorkCpRecord();
    }

    public function getRecordWithList($list){
        if (empty($list)){
            return [];
        }

        $workIdArr = array_column($list,'id');
        $map = [
            ['work_id','in',$workIdArr]
        ];
        $recordList = $this->model->getNormalListByWhereWithOrder($map,'create_time asc');
        if ($recordList->isEmpty()){
            foreach ($list as $k=>$v){
                $list[$k]['price_record'] = ['population_origin'=>$v['population'],'price_origin'=>$v['price']];
            }
            return $list;
        }
        $recordList = $recordList->toArray();

        $arr = [];
        foreach ($recordList as $k=>$v){
            !isset($arr[$v['work_id']]) && $arr[$v['work_id']] = $v;
        }
        foreach ($list as $k=>$v){
            $list[$k]['price_record'] = $arr[$v['id']] ?? ['population_origin'=>$v['population'],'price_origin'=>$v['price']];
        }
        return $list;
    }

    public function getNormalByWhere($where,$field='*'): array
    {
        $data = $this->model->getNormalByWhere($where, $field);
        if (!$data) {
            return [];
        }
        return $data->toArray();
    }
}
